package net.sevear.chaos.utils
{
	import flash.display.Bitmap;
	import flash.events.EventDispatcher;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.ObjectEncoding;
	import flash.utils.ByteArray;
	
	import net.sevear.chaos.vo.TrackInfo;

	public class SongID3InfoEidtor extends EventDispatcher
	{
		public function SongID3InfoEidtor():void{
			
		}
		
		public function updateID3(fileLocation:String,info:TrackInfo,coverBmp:Bitmap=null):void{
			var id3ByteArray:ByteArray = generateCompleteID3(info,null);
			//test
			var fl:File = File.desktopDirectory.resolvePath(fileLocation);
			var fs:FileStream = new FileStream();
			try{
				var oldMp3File:ByteArray = new ByteArray();
				//open file in write mode
				fs.open(fl,FileMode.READ);
//				fs.writeBytes(id3ByteArray);
				fs.readBytes(oldMp3File,0,fs.bytesAvailable);
				//close the file
				fs.close();
				
				fl = File.desktopDirectory.resolvePath(fileLocation);
				fs = new FileStream();
				fs.open(fl,FileMode.WRITE);
				fs.writeBytes(id3ByteArray,0,id3ByteArray.length);
				fs.writeBytes(oldMp3File,0,oldMp3File.length);
				fs.close();
			}catch(e:Error){
				trace(e.message);
			}
		}
		
		private function generateCompleteID3(info:TrackInfo,albumImg:Bitmap=null):ByteArray{
			var id3ByteArray:ByteArray = new ByteArray();
			//D3v2 Identifier
			id3ByteArray.writeMultiByte("ID3","utf-8");
			//ID3v2 Version
			id3ByteArray.writeByte(parseInt("03"));
			id3ByteArray.writeByte(parseInt("00"));
			//ID3v2 Flags
			id3ByteArray.writeByte(0);
			//ID3v2 Size
			var id3Size:int = 0;
			
			var nameFrameBtye:ByteArray = addFrame("TIT2",info.name);
			var artistFrame:ByteArray = addFrame("TPE1",info.artistName);
			var albumFrame:ByteArray = addFrame("TALB",info.albumName);
			
			var idsSize:int = nameFrameBtye.length+artistFrame.length+albumFrame.length+1;
			if(idsSize>=256){
				idsSize+=256;
			}
			if(idsSize>=56636){
				idsSize+=56636;
			}
			if(idsSize>=256*256*256){
				idsSize+=56636;
			}
			if(idsSize>=256*256*256*256){
				idsSize = idsSize^0x9fffffff;
			}
			
			id3ByteArray.writeInt(idsSize);
			id3ByteArray.writeBytes(nameFrameBtye,0,nameFrameBtye.length);
			id3ByteArray.writeBytes(artistFrame,0,artistFrame.length);
			id3ByteArray.writeBytes(albumFrame,0,albumFrame.length);
			id3ByteArray.writeByte(0);
//			var idsSizeByte:ByteArray = new ByteArray();
			return id3ByteArray;
		}	
		
		private function addFrame(frameID:String,name:String):ByteArray{
			var nameFrameBtye:ByteArray = new ByteArray();
			nameFrameBtye.writeMultiByte(frameID,"utf-8");
			
			var nameDataByte:ByteArray = new ByteArray();
			nameDataByte.writeByte(1);//表示是unicode编码
			nameDataByte.writeMultiByte(name,"unicode");
			nameDataByte.position = 0;
			var nameByteLen:int = nameDataByte.length;
			
			//frame size
			nameFrameBtye.writeInt(nameByteLen);
			//frame flags
			nameFrameBtye.writeByte(0);
			nameFrameBtye.writeByte(0);
			//
			nameFrameBtye.writeBytes(nameDataByte,0,nameDataByte.length);
			
			return nameFrameBtye;
		}
		
//		private
		
//		public function readID3
		//TODO:
		public function checkCharSet(content:ByteArray):String{
			return "utf-8";
		}
	}
}